package kr.co.handysoft.project.smartPolice.composite;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import kr.co.handysoft.common.registry.SmartPoliceRegistry;
import kr.co.handysoft.common.vo.ServerInformation;
import kr.co.handysoft.project.smartPolice.composite.detail.DetailStatusComposite;
import kr.co.handysoft.project.smartPolice.dialog.CreateReceiverServerDialog;
import kr.co.handysoft.smartPolice.server.ZoneHandlerThread;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.wb.swt.SWTResourceManager;

public class SystemStatusComposite extends Composite {

	private Tree tree;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public SystemStatusComposite(final Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
		setLayout(new FormLayout());
		
		Composite composite = new Composite(this, SWT.NONE);
		FormData fd_composite = new FormData();
		fd_composite.top = new FormAttachment(0);
		fd_composite.height = 35;
		fd_composite.left = new FormAttachment(0);
		composite.setLayoutData(fd_composite);
		
		SashForm sashForm = new SashForm(this, SWT.NONE);
		fd_composite.right = new FormAttachment(sashForm, 0, SWT.RIGHT);
		FormData fd_sashForm = new FormData();
		fd_sashForm.top = new FormAttachment(composite, 3);
		fd_sashForm.bottom = new FormAttachment(100, -3);
		
		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new CreateReceiverServerDialog(parent.getShell(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL).open();
			}
		});
		btnNewButton.setImage(SWTResourceManager.getImage(SystemStatusComposite.class, "/com/famfamfam/slik/icons/server_add.png"));
		btnNewButton.setBounds(5, 5, 142, 25);
		btnNewButton.setText("\uC11C\uBE44\uC2A4 \uCD94\uAC00 \uC0DD\uC131");
		fd_sashForm.left = new FormAttachment(0, 3);
		fd_sashForm.right = new FormAttachment(100, -3);
		sashForm.setLayoutData(fd_sashForm);
		
		tree = new Tree(sashForm, SWT.BORDER);
		
		SmartPoliceRegistry.setReceiverTree(tree);
		Menu menu = new Menu(tree);
		tree.setMenu(menu);
		
		MenuItem mntmTj = new MenuItem(menu, SWT.NONE);
		mntmTj.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TreeItem[] itemArray = tree.getSelection();
				if (itemArray.length > 0){
					String serviceName = (String)itemArray[0].getData("name");
					itemArray[0].setText("Service " + serviceName + "- RUNNING");
					ZoneHandlerThread zThread = (ZoneHandlerThread)itemArray[0].getData("service");
					
					if (!zThread.isAlive()){
						zThread= new ZoneHandlerThread(Integer.parseInt(serviceName), itemArray[0]);
						itemArray[0].setData("service",zThread);
					}
					
					SmartPoliceRegistry.setServerFlag(Integer.parseInt(serviceName), true);
					zThread.start();
				}
			}
		});
		mntmTj.setImage(SWTResourceManager.getImage(SystemStatusComposite.class, "/com/famfamfam/slik/icons/resultset_next.png"));
		mntmTj.setText("\uC11C\uBE44\uC2A4 \uC2DC\uC791");
		
		MenuItem menuItem = new MenuItem(menu, SWT.NONE);
		menuItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TreeItem[] itemArray = tree.getSelection();
				if (itemArray.length > 0){
					String serviceName = (String)itemArray[0].getData("name");
					itemArray[0].setText("Service " + serviceName + "- STOP");
					ZoneHandlerThread zThread = (ZoneHandlerThread)itemArray[0].getData("service");
					zThread.stopListening();
				}
			}
		});
		menuItem.setImage(SWTResourceManager.getImage(SystemStatusComposite.class, "/com/famfamfam/slik/icons/stop.png"));
		menuItem.setText("\uC11C\uBE44\uC2A4 \uC911\uC9C0");
		
		MenuItem mntmNewItem = new MenuItem(menu, SWT.NONE);
		mntmNewItem.setImage(SWTResourceManager.getImage(SystemStatusComposite.class, "/com/famfamfam/slik/icons/cross.png"));
		mntmNewItem.setText("\uC11C\uBE44\uC2A4 \uC0AD\uC81C");
		
		new MenuItem(menu, SWT.SEPARATOR);
		
		MenuItem mntmNewItem_1 = new MenuItem(menu, SWT.NONE);
		mntmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
			}
		});
		mntmNewItem_1.setImage(SWTResourceManager.getImage(SystemStatusComposite.class, "/com/famfamfam/slik/icons/control_stop.png"));
		mntmNewItem_1.setText("\uD074\uB77C\uC774\uC5B8\uD2B8 \uAC15\uC81C \uC885\uB8CC");
		
		new MenuItem(menu, SWT.SEPARATOR);
		
		MenuItem menuItem_1 = new MenuItem(menu, SWT.NONE);
		menuItem_1.setImage(SWTResourceManager.getImage(SystemStatusComposite.class, "/com/famfamfam/slik/icons/script.png"));
		menuItem_1.setText("\uC0C1\uD0DC\uBCF4\uAE30");
		
		CTabFolder tabFolder = new CTabFolder(sashForm, SWT.BORDER | SWT.CLOSE);
		tabFolder.setTabHeight(23);
		tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
		sashForm.setWeights(new int[] {1, 3});

		startListener(SmartPoliceRegistry.getServerInfoCollection(), tree);

	}
	
	private void startListener(HashMap<String, ServerInformation> map, Tree tree) {
		Set<String> keyMap = map.keySet();
		Iterator<String> keyIterator = keyMap.iterator();
		while(keyIterator.hasNext()){
			String keyString = (String)keyIterator.next();
			ServerInformation information = map.get(keyString);
			TreeItem trtmService = new TreeItem(tree, 0);
			
			trtmService.setText("Service " + information.getPort() + "- STOP");
			trtmService.setImage(SWTResourceManager.getImage(SystemStatusComposite.class, "/com/famfamfam/slik/icons/server.png"));
			trtmService.setExpanded(true);
			trtmService.setData("name", information.getPort());
			trtmService.setData("service", new ZoneHandlerThread(Integer.parseInt(information.getPort()), trtmService));
			SmartPoliceRegistry.setServerFlag(Integer.parseInt(information.getPort()), true);
		}
	}

	public void close(){
		TreeItem[] itemArray = tree.getItems();
		for (TreeItem tItem : itemArray){
			ZoneHandlerThread zThread = (ZoneHandlerThread)tItem.getData("service");
			zThread.stopListening(); 
		}
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
